package com.study.flink.java.day06_exactly;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.runtime.state.filesystem.FsStateBackend;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.CheckpointConfig;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;

import java.util.Properties;

//KafkaSource->并行的source
public class KafkaSourceV2 {

    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        // 并行度4
        env.setParallelism(4);
        // 开启Checkpointing，同时开启重启策略
        env.enableCheckpointing(5000L);
        // 设置重启策略
        env.getConfig().setRestartStrategy(org.apache.flink.api.common.restartstrategy.RestartStrategies.fixedDelayRestart(2, 2000));
        // 设置StateBackend
        env.setStateBackend(new FsStateBackend("file:///D:\\IDEA\\flink-study\\dir\\day06\\backend"));
        // 取消任务checkpoint不删除
        env.getCheckpointConfig().enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);
        // 设置checkpoint的模式
        env.getCheckpointConfig().setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE);

        // kafka的partitions有3个，对应有3个source
        Properties props = new Properties();
        props.setProperty("bootstrap.servers", "node02:9092"); //kafka的broker地址
        props.setProperty("group.id", "gid-wc12");//指定组ID
        props.setProperty("auto.offset.reset", "earliest");//如果没有记录偏移量，第一次从最开始消费
        props.setProperty("enable.auto.commit", "false");//自己管理偏移量

        // 用kafka的并行source，每一个组都要满足条件才会触发
        FlinkKafkaConsumer<String> kafkaSource = new FlinkKafkaConsumer<>("wc12", new SimpleStringSchema(), props);

        // Flink Checkpoint成功后还要向kafka特殊的topic中写入偏移量
        // 特殊topic有两个作用：1.监控用，2.任务重启恢复数据用，如果没有指定savePoint，则从特殊topic中的偏移量恢复消费数据
        kafkaSource.setCommitOffsetsOnCheckpoints(false);

        // source
        DataStream<String> lines = env.addSource(kafkaSource);

        SingleOutputStreamOperator<Tuple2<String, Integer>> wordAndCount = lines.map(new MapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public Tuple2<String, Integer> map(String s) throws Exception {
                // (单词,次数)
                return Tuple2.of(s, 1);
            }
        });
        // 聚合计算wordcount
        SingleOutputStreamOperator<Tuple2<String, Integer>> summed = wordAndCount.keyBy(0).sum(1);

        // sink
        summed.print();

        env.execute("KafkaSource-java");

    }




}
